/*******************************************************************************
 *
 *    Copyright (c) Baina Info Tech Co. Ltd
 *
 *    AndroidSDK
 *
 *    SNSUtil
 *
 *    @author: zuochen
 *    @since:  2011-11-7
 *    @version: 1.0
 *
 ******************************************************************************/

package com.sdk.sns;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.widget.Toast;

/**
 * SNSUtil of AndroidSDK.
 * 
 * @author zuochen
 */

public class SNSUtil {
	public static void share(Context context, String subjcet, String body) {
		final Uri mUri = Uri.parse("mailto:");
		final Intent intent = new Intent(Intent.ACTION_SEND, mUri);
		intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "" });
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_SUBJECT, subjcet);
		intent.putExtra(Intent.EXTRA_TEXT, body);
		intent.putExtra(Intent.EXTRA_CC, new String[] { "" });
		context.startActivity(Intent.createChooser(intent,
				"Choose Email Client"));
	}

	public static boolean isPackageInstalled(Context context, String packageName) {
		boolean r = false;
		final PackageManager pm = context.getPackageManager();
		try {
			final PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
			if (packageInfo != null) {
				r = true;
			}
		} catch (final NameNotFoundException e) {
		}
		return r;
	}

	public static void feedBack(Context context, String subjcet, String body,
			String feedbackAddress) {
		final Uri mUri = Uri.parse("mailto:");
		final Intent intent = new Intent(Intent.ACTION_SEND, mUri);
		intent.putExtra(Intent.EXTRA_EMAIL, new String[] { feedbackAddress });
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_SUBJECT, subjcet);
		intent.putExtra(Intent.EXTRA_TEXT, body);
		intent.putExtra(Intent.EXTRA_CC, new String[] { "" });
		context.startActivity(Intent.createChooser(intent,
				"Choose Email Client"));
	}

	/**
	 * 分享附件
	 * 
	 * @param context
	 * @param filePath
	 */
	public static void shareAttachment(Context context, String filePath) {
		File file = new File(filePath); // 附件文件地址
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
		if (file.getName().endsWith(".gz")) {
			intent.setType("application/x-gzip");
		} else if (file.getName().endsWith(".txt")) {
			intent.setType("text/plain");
		} else {
			intent.setType("application/octet-stream");
		}
		context.startActivity(Intent.createChooser(intent,
				"Choose Share Client"));
	}

	public static void doRating(Context context, String packageName) {
		try {
			final Uri marketUri = Uri.parse("market://details?id="
					+ packageName);
			final Intent intent = new Intent(Intent.ACTION_VIEW, marketUri);
			intent.addCategory(Intent.CATEGORY_BROWSABLE);
			context.startActivity(intent);
		} catch (Exception e) {
			Toast.makeText(context, "Can't find any Market Client",
					Toast.LENGTH_SHORT).show();
		}

	}

	/**
	 * 获取更多的应用
	 * @param context
	 * @param developName
	 */
	public static void getMoreApp(Context context, String developName) {
		try {
			final Uri marketUri = Uri.parse("market://search?q=pub:"
					+ developName);
			final Intent intent = new Intent(Intent.ACTION_VIEW, marketUri);
			intent.addCategory(Intent.CATEGORY_BROWSABLE);
			context.startActivity(intent);
		} catch (Exception e) {
			Toast.makeText(context, "Can't find any Market Client",
					Toast.LENGTH_SHORT).show();
		}
	}
}
